package perso.android.savethemage.v1;


import java.util.ArrayList;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.modifier.ColorBackgroundModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.svg.opengl.texture.atlas.bitmap.SVGBitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.widget.RelativeLayout;


import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;



public class LoadingGameActivity extends SimpleBaseGameActivity {
	
		protected Engine engine;
		protected PhysicsWorld physEngine;
	 
		private BitmapTextureAtlas mBitmapTextureAtlasBis;
		private TiledTextureRegion mLoadTextureRegion;
		private Scene splashScreen;
		private Camera mCamera;
		private Font mFont;
		private BitmapTextureAtlas mFontTexture;
		private ITiledTextureRegion mSVGTestTextureRegionsLoad;
		private BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlasLoad;
		
		protected ShakeCamera sCamera = new ShakeCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
	 	 
		protected static  float CAMERA_WIDTH = 320, CAMERA_HEIGHT = 480;
		protected static final int AD_SIZE_Y = 50;
		protected static final int PHYSICS_TIME_STEP = 60;
	 
		private int sizeLoad;

	 

		public void onLoadResources() {
			
			
	    	 
		}
	 

		public Scene onCreaScene() {
			 this.mEngine.registerUpdateHandler(new FPSLogger());

		    	
		    splashScreen = new Scene();
		    //splashScreen.setBackground(new ColorBackground(0.604f, 0.7674f, 0.9f));
		    splashScreen.setBackground(new Background(1, 1, 1));
		    
		    	 
		    final AnimatedSprite load = new AnimatedSprite(CAMERA_WIDTH - sizeLoad, CAMERA_HEIGHT - sizeLoad, sizeLoad, sizeLoad, this.mSVGTestTextureRegionsLoad, this.getVertexBufferObjectManager());
		    	 
		    final Text fpsText = new Text(CAMERA_WIDTH - (sizeLoad*5.60f), 
		    			 CAMERA_HEIGHT - sizeLoad*0.9f,  this.mFont, "CHARGEMENT", "Chargement".length(), this.getVertexBufferObjectManager());
		    
		    fpsText.setColor(0.9f, 0.2f, 0.8f, 0.6f);
		    splashScreen.attachChild(fpsText);
		    
			load.animate(60);
		    	 
		
				 
		    splashScreen.attachChild(load);
		    	 
		
			IAsyncCallback callback = new IAsyncCallback() {
	 
				public void workToDo() {
					assetsToLoad();
				}
	 
				public void onComplete() {
					unloadLoadingScene();
					engine.setScene(onAssetsLoaded());
				}
			};
	 
			new AsyncTaskLoader().execute(callback);
	 
			return splashScreen;
		}
	 
		private void unloadLoadingScene(){	 
		}
	 
		protected   Scene onAssetsLoaded() {
			return null;
		}
	
		protected  void assetsToLoad() {
		}

		public void onLoadComplete() {			
		}

		public org.andengine.engine.options.EngineOptions onCreateEngineOptions() {
			DisplayMetrics metrics = new DisplayMetrics();
    		getWindowManager().getDefaultDisplay().getMetrics(metrics);

    		CAMERA_HEIGHT = metrics.heightPixels;
    		CAMERA_WIDTH = metrics.widthPixels;
    		
    		sizeLoad = (int)(CAMERA_WIDTH * 0.13f);
    		
    	   this.sCamera = new ShakeCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    	    
			
			return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CameraDim.CAMERA_WIDTH, CameraDim.CAMERA_HEIGHT), this.sCamera);
		}

		@Override
		protected void onCreateResources() {
			BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
						
	    	SVGBitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
	    	 
	    	this.mBuildableBitmapTextureAtlasLoad = new BuildableBitmapTextureAtlas(this.getTextureManager(), 256,256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	    	 
	    	this.mSVGTestTextureRegionsLoad = SVGBitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBuildableBitmapTextureAtlasLoad, this, "loadSpin.svg", 256, 128, 8, 4 );

	        try {
				this.mBuildableBitmapTextureAtlasLoad.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
				this.mBuildableBitmapTextureAtlasLoad.load();
			} catch (final TextureAtlasBuilderException e) {
				Log.e("Misa.Load", "Probleme");
			}
	        
	    	//////////////////////////////////////////////////////////////////////////// 
	    	
	    	this.mEngine.getTextureManager().loadTexture(this.mBuildableBitmapTextureAtlasLoad);
	    	 
	    	 
	        FontFactory.setAssetBasePath("gfx/");

	 		final BitmapTextureAtlas fontPersoTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
	 		
	 		this.mFont = FontFactory.createFromAsset(this.getFontManager(), fontPersoTexture, this.getAssets(), "casual_tossed.ttf", (int)(CAMERA_WIDTH/15), true, Color.WHITE);
	 		
	 
	 		this.mFont.load();
	 		//this.getEngine().getTextureManager().loadTexture(fontPersoTexture);

	 		//this.getEngine().getFontManager().loadFont(this.mFont);
			
		}

		@Override
		protected org.andengine.entity.scene.Scene onCreateScene() {
			// TODO Auto-generated method stub
			return null;
		}
	 
	}